package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.DispatchConfig;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.DispatchConfigService;
import com.tbit.uqbike.service.business.impl.SensitivenessServiceImpl;
import com.tbit.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @Author HZW
 * @ClassName DispatchConfigController
 * @Description
 * @date 2022/3/15 17:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/dispatchConfig")
public class DispatchConfigController {
    @Resource
    private DispatchConfigService dispatchConfigService;
    @Autowired
    private SensitivenessServiceImpl sensitivenessService;

    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.DISPATCH_CONFIG)
    public JsonResult edit(DispatchConfig dispatchConfig) {
        DispatchConfig config = dispatchConfigService.getByAccountId(dispatchConfig.getAccountId());
        if (Objects.nonNull(config)) {
            dispatchConfigService.update(dispatchConfig);
        } else {
            dispatchConfigService.insert(dispatchConfig);
        }
        sensitivenessService.sendMessage(dispatchConfig.getAccountId(),1);
        return JsonResult.ok();
    }

    @RequestMapping("/getByAccount")
    public JsonResult getByAccount(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        DispatchConfig config = dispatchConfigService.getByAccountId(accountId);
        if (NullUtils.isEmpty(config)) {
            config = new DispatchConfig();
            //多少小时产生订单
            config.setRideTime(24);
            //两个工单内是否产生订单，0：不产生
            config.setRide(0);
            //订单距离
            config.setRideDistance(0);
            //记录间隔时间
            config.setLogTime(0);
            //调度闲置时长
            config.setDispatchLeave(0);
        }
        return JsonResult.ok(config);
    }

}
